package com.itheima.algorithm.binarysearchtree;

import java.util.Comparator;

/**
 * @author: TylerZhong
 * @description: 二叉搜索树，泛型 key 版本
 */
public class BSTTree2<K extends Comparable<K>,V> {
    public BSTNode<K,V> root;

    public static class BSTNode<K,V> {
        K key;
        V value;
        BSTNode<K,V> left;
        BSTNode<K,V> right;

        public BSTNode(K key) {
            this.key = key;
        }

        public BSTNode(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public BSTNode(K key, V value, BSTNode<K,V> left, BSTNode<K,V> right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    public BSTTree2() {
    }

    public BSTTree2(BSTNode<K,V> root) {
        this.root = root;
    }

    public Object get(K key) {
        BSTNode<K,V> node = root;
        while (node != null) {
            int result = key.compareTo(node.key);
            if (result < 0) {
                node = node.left;
            } else if (result > 0) {
                node = node.right;
            } else {
                return node.value;
            }
        }
        return null;
    }


}
